package com.example.jixinlai203.jixinlai203.model;

import com.example.jixinlai203.jixinlai203.been.VideoBean;
import com.example.jixinlai203.jixinlai203.iface.VideoListener;
import com.example.jixinlai203.jixinlai203.iface.Videoiface;
import com.example.jixinlai203.jixinlai203.service.VideoService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


//model 获取文章数据
public class VideoModel implements Videoiface {
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    //构造函数
    public VideoModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //获取文章数据
    @Override
    public void getResultList(String video, int page, String sessionID, final VideoListener videoListener) {
        //使用Retrofit-2
        VideoService videoService=retrofit.create(VideoService.class);
        Call<List<VideoBean>> call
                =videoService.getResult(video,page,sessionID);
        //使用Retrofit-3
        call.enqueue(new Callback<List<VideoBean>>() {
            @Override
            public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                if(response.isSuccessful()&&response!=null){
                    videoListener.onResponse(response.body());
                }
                else
                    videoListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                videoListener.onFail(t.toString());
            }
        });
    }
}
